/*
 1 查看某一具体订单
 2 批量修改具体订单的商品数量
 3 接受订单
 4 取消接受订单
 5 接受退货
 */

package org.csu.goodpartner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.goodpartner.VO.LineItemVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.Inventory;
import org.csu.goodpartner.entity.LineItem;
import org.csu.goodpartner.entity.Order;
import org.csu.goodpartner.persistence.InventoryMapper;
import org.csu.goodpartner.persistence.LineItemMapper;
import org.csu.goodpartner.persistence.OrderMapper;
import org.csu.goodpartner.service.LineItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LineItemServiceImpl implements LineItemService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private LineItemMapper lineItemMapper;

    //查看某一具体订单
    @Override
    public CommonResponse<List<LineItemVO>> getLineItemByOrderId(int orderId) {
        List<LineItemVO> lineItemVOList = lineItemMapper.getLineItemByOrderId(orderId);
        if(lineItemVOList.size() == 0){
            lineItemVOList.add(lineItemMapper.getLineItem(orderId));
        }
        for(LineItemVO lineItemVO : lineItemVOList) {
            System.out.println(lineItemVO.getAddrnum());
            //lineItemVO.setAddr(lineItemMapper.getAddrByAddrNum(lineItemVO.getAddrnum()));
            //lineItemVO.setPhone(lineItemMapper.getPhoneByAddrNum(lineItemVO.getAddrnum()));
        }
        return CommonResponse.createForSuccess(lineItemVOList);
    }

    //批量修改具体订单的商品数量
    @Override
    public CommonResponse updateQuanties(List<LineItem> lineItemList) {
        int i = 1;
        for(LineItem lineItem: lineItemList){
            int orderId = lineItem.getOrderid();
            int itemId = lineItem.getItemid();
            System.out.println(orderId + "===========" + itemId);
            int shopId = orderMapper.selectById(orderId).getShopid();

            int change = 0;
            int inventory = lineItemMapper.getInventoryByItemIdAndShopId(itemId, shopId);
            int quantity = lineItem.getQuantity();//新修改的数量
            int p_quantity = lineItemMapper.getQuantityByItemIdAndOrderId(itemId,orderId);//没有修改的，存在数据库里面的数量
            System.out.println("这里是库存："+p_quantity+"这里是修改后的数据："+quantity);
            if(p_quantity > quantity){//quantity-- inventory++
                change = p_quantity - quantity;
                inventory = inventory + change;
            }
            else if(p_quantity < quantity){
                change = quantity - p_quantity;
                if((inventory - change) < 0){
                    return CommonResponse.createForError("第"+i+"行商品库存不足，请重新修改");
                }
                else{
                    inventory = inventory - change;
                }
            }

            QueryWrapper<LineItem> queryMapper1 = new QueryWrapper<>();
            queryMapper1.eq("orderid",orderId);
            queryMapper1.eq("itemid",itemId);
            LineItem lineItem1 = lineItemMapper.selectOne(queryMapper1);
            lineItem1.setQuantity(quantity);
            lineItemMapper.update(lineItem1,queryMapper1);

            QueryWrapper<Inventory> queryMapper2 = new QueryWrapper<>();
            queryMapper2.eq("shopid",shopId);
            queryMapper2.eq("itemid",itemId);
            Inventory inventory1 = inventoryMapper.selectOne(queryMapper2);
            inventory1.setQuantity(inventory);
            inventoryMapper.update(inventory1,queryMapper2);

            i++;
        }
        return CommonResponse.createForSuccessMessage("修改成功");
    }

    /*
     0：未发货；1：已发货；2：取消订单；3：退货；4：待商家确认订单；
     5：待商家确认退货；6：取消退货；7：初始值；8：待收货
    */
    //接受订单 4-0 inventory--
    @Override
    public CommonResponse receiveOrder(int orderId) {
        List<LineItemVO> lineItemVOList = lineItemMapper.getLineItemByOrderId(orderId);
        if(lineItemVOList.size() == 0){
            //lineItemVOList = new List<LineItemVO>();
            lineItemVOList.add(lineItemMapper.getLineItem(orderId));
        }
        for(LineItemVO lineItemVO: lineItemVOList){
            int itemId = lineItemVO.getItemid();
            System.out.println(itemId+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@222");
            int shopId = orderMapper.selectById(orderId).getShopid();

            int inventory = lineItemMapper.getInventoryByItemIdAndShopId(itemId, shopId);
            int quantity = lineItemVO.getQuantity();
            if(inventory >= quantity){
                inventory = inventory - quantity;

                QueryWrapper<Inventory> queryMapper = new QueryWrapper<>();
                queryMapper.eq("shopid",shopId);
                queryMapper.eq("itemid",itemId);
                Inventory inventory_class = inventoryMapper.selectOne(queryMapper);
                inventory_class.setQuantity(inventory);
                inventoryMapper.update(inventory_class,queryMapper);

                QueryWrapper<Order> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("orderid", orderId);
                Order order = orderMapper.selectOne(queryWrapper2);
                order.setStatus(0);
                orderMapper.update(order, queryWrapper2);
            }
            else{
                return CommonResponse.createForError("商品库存不足!");
            }
        }
        return CommonResponse.createForSuccessMessage("确认接受"+orderId+"号订单");
    }

    //取消接受订单 4-2或0-2 inventory++
    @Override
    public CommonResponse cancelOrder(int orderId) {
        List<LineItemVO> lineItemVOList = lineItemMapper.getLineItemByOrderId(orderId);
        if(lineItemVOList.size() == 0){
            lineItemVOList.add(lineItemMapper.getLineItem(orderId));
        }
        for(LineItemVO lineItemVO: lineItemVOList){
            int itemId = lineItemVO.getItemid();
            int shopId = orderMapper.selectById(orderId).getShopid();

            int inventory = lineItemMapper.getInventoryByItemIdAndShopId(itemId, shopId);
            int quantity = lineItemVO.getQuantity();

            inventory = inventory + quantity;

            QueryWrapper<Inventory> queryMapper = new QueryWrapper<>();
            queryMapper.eq("shopid",shopId);
            queryMapper.eq("itemid",itemId);
            Inventory inventory_class = inventoryMapper.selectOne(queryMapper);
            inventory_class.setQuantity(inventory);
            inventoryMapper.update(inventory_class,queryMapper);

            QueryWrapper<Order> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("orderid", orderId);
            Order order = orderMapper.selectOne(queryWrapper2);
            order.setStatus(2);
            orderMapper.update(order, queryWrapper2);
        }
        return CommonResponse.createForSuccessMessage("确认取消接受"+orderId+"号订单");
    }

    //接受退货：5-3 inventory++
    @Override
    public CommonResponse agreeRefund(int orderId) {
        List<LineItemVO> lineItemVOList = lineItemMapper.getLineItemByOrderId(orderId);
        if(lineItemVOList.size() == 0){
            lineItemVOList.add(lineItemMapper.getLineItem(orderId));
        }
        for(LineItemVO lineItemVO: lineItemVOList){
            int itemId = lineItemVO.getItemid();
            int shopId = orderMapper.selectById(orderId).getShopid();

            int inventory = lineItemMapper.getInventoryByItemIdAndShopId(itemId, shopId);
            int quantity = lineItemVO.getQuantity();

            inventory = inventory + quantity;

            QueryWrapper<Inventory> queryMapper = new QueryWrapper<>();
            queryMapper.eq("shopid",shopId);
            queryMapper.eq("itemid",itemId);
            Inventory inventory_class = inventoryMapper.selectOne(queryMapper);
            inventory_class.setQuantity(inventory);
            inventoryMapper.update(inventory_class,queryMapper);

            QueryWrapper<Order> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("orderid", orderId);
            Order order = orderMapper.selectOne(queryWrapper2);
            order.setStatus(3);
            orderMapper.update(order, queryWrapper2);
        }
        return CommonResponse.createForSuccessMessage("确认接受"+orderId+"号订单的退货请求");
    }

}
